package com.wgl.sms.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wgl.model.vo.msm.MsmVo;
import com.wgl.model.vo.order.OrderMqVo;
import com.wgl.sms.service.MsmService;
import org.apache.rocketmq.client.consumer.DefaultLitePullConsumer;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author wgl
 * @version 1.0.0
 * @ClassName MsmReceiver.java
 * @Description TODO
 * @createTime 2022年02月14日 14:51:00
 */
@Component
public class MsmReceiver {

    @Resource
    private MsmService msmService;
    @Value("${rocketmq.nameserver}")
    private String nameserver;


    @PostConstruct
    public void send(){
        DefaultMQPushConsumer yygh_xf = new DefaultMQPushConsumer("yygh_msm");
        //连接NameServer
        yygh_xf.setNamesrvAddr(nameserver);
        try {
            yygh_xf.subscribe("yygh", "fdx");
            yygh_xf.registerMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                    msgs.forEach(messageExt -> {
                        MsmVo o =  JSONObject.parseObject(new String(messageExt.getBody()),MsmVo.class);
                        System.out.println(o);
                        msmService.sendOrder("","下单/取消成功!!");
                        System.out.println("msm消费！！");
                    });
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            });
            yygh_xf.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }

    }


}
